package com.atguigu04._throw;

/**
 * ClassName: ThrowTest
 * Package: com.atguigu04._throw
 * Description:
 *
 * @Author ZY
 * @Create 22:16
 * @Version 1.0
 */
public class ThrowTest {
    public static void main(String[] args) {

        Student s1 = new Student();

        try{
            s1.regist(10);
            s1.regist(-10);
            System.out.println(s1);
        }catch (Exception e){
            e.printStackTrace();
        }

    }

}


class Student{
    int id;


//    public void regist(int id){
//        if(id > 0){
//            this.id = id;
//        }else{
////            System.out.println("输入的id非法");
//            //手动抛出异常对象
////            throw new RuntimeException("输入的id非法");
//        }
//    }


    public void regist(int id) throws Exception{
        if(id > 0){
            this.id = id;
        }else{
//            System.out.println("输入的id非法");
            //手动抛出异常对象
//            throw new RuntimeException("输入的id非法");
            throw new Exception("输入的id非法");
        }
    }

    @Override
    public String toString() {
        return "Student{" +
                "id=" + id +
                '}';
    }
}
